import { Navigate, useLocation } from 'react-router-dom';

const PrivateRoute = ({ children }) => {
  const location = useLocation();
  const token = localStorage.getItem('token'); // 从localStorage获取token

  // 如果没有token，则重定向到登录页
  if (!token) {
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  // 如果有token，则正常渲染子组件
  return children;
};

export default PrivateRoute;
